package com.zkjl.protect_business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.IdUtil;
import com.zkjl.protect_business.common.UserThreadLocal;
import com.zkjl.protect_business.dto.PageBaseDTO;
import com.zkjl.protect_business.dto.SortDTO;
import com.zkjl.protect_business.entity.Log;
import com.zkjl.protect_business.entity.User;
import com.zkjl.protect_business.repository.LogRepository;
import com.zkjl.protect_business.service.LogService;
import com.zkjl.protect_business.service.OrganizationService;
import com.zkjl.protect_business.utils.QueryUtil;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.zkjl.protect_business.common.Consts.LOG_TYPE_BUSINESS_OPERATION;
import static com.zkjl.protect_business.common.Consts.LOG_TYPE_OPERATION;

@Service
public class LogServiceImpl implements LogService {

    @Autowired
    LogRepository logRepository;
    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    OrganizationService organizationService;


    @Override
    public Log save(String name, String targetName,String operationType) {

        Log log = new Log();
        log.setId(IdUtil.simpleUUID());
        log.setName(name);
        log.setContent(targetName);
        log.setLogType(LOG_TYPE_BUSINESS_OPERATION);
        log.setOperationType(operationType);

        User user = UserThreadLocal.get();
        if (user != null) {
            log.setBaseInfo();
        }

        log.setCreateTime(System.currentTimeMillis());

        Log save = logRepository.save(log);

        return save;
    }

    @Override
    public PageImpl page(PageBaseDTO pageBaseDTO, SortDTO sortDTO, String logType, String operationType) {

        NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder();
        BoolQueryBuilder qb = QueryBuilders.boolQuery();

        Pageable pageable = QueryUtil.build(qb, queryBuilder, pageBaseDTO, sortDTO, Log.class);

        if (StringUtils.isNotBlank(operationType)) {
            qb.filter(new QueryStringQueryBuilder("operationType").field("operation_type").defaultOperator(Operator.AND));
        }

        if (StringUtils.isNotBlank(logType)) {
            qb.filter(new QueryStringQueryBuilder(logType).field("log_type").defaultOperator(Operator.AND));
        }

        //只能获取当前机构下的日志
        List<String> list = organizationService.listChildrenIdsAndOneself(UserThreadLocal.getOrgId());
        if (CollUtil.isEmpty(list)) {
            return new PageImpl(null, pageable, 0);
        }
        qb.filter(new TermsQueryBuilder("create_org_id",list));

        queryBuilder.withQuery(qb);
        SearchHits<Log> search1 = elasticsearchRestTemplate.search(queryBuilder.build(), Log.class);
        List<Log> collect = search1.stream().map(SearchHit::getContent).collect(Collectors.toList());

        long total = search1.getTotalHits();

        PageImpl page = new PageImpl(collect, pageable, total);

        return page;

    }
}
